#This represents the stack for our interpriter.
from errors import * #load compilation exceptions so we can handle the user's programming errors.

class Stack:
 def __init__(self): 
  self.stack=[] #use a simple list.

 def push(self, atom):
  self.stack.append(atom)

 def pop(self):
  if len(self.stack) > 0: #check if something is there to pop.
   return self.stack.pop()
  else:
   raise CompilingErrorException("You tried to use a value from the stack when the stack was empty.") #handle programming error.

 def count(self): #number of items on stack.
  return len(self.stack)

 def npop(self, n): #get more than 1 argument from stack. Returns as tuple, good for passing to functions.
  r=[self.pop() for i in range(n)] #get items, checking for errors.
  r.reverse() #using pop, we have reversed the order.
  return tuple(r)

 def peak(self): #look at top argument without removing it.
  a=self.pop() #handling programming errors, get the item on the stack.
  self.push(a) #put it back.
  return a #return what it was.
